﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AllenalaMente.Games.Exchanges
{
    public partial class Cover : UserControl
    {
        public Point position;

        public Cover()
        {
            InitializeComponent();            
        }

        public bool isMoving = false;

        public void MoveTo(Point p)
        {
            Move((int)0, (int)0, (int)(p.X - position.X), (int)0);
        }

        public void Move(int FromX, int FromY, int ToX, int ToY)
        {
            if (isMoving) return;

            isMoving = true;

            EasingFunctionBase[] ef = new EasingFunctionBase[]{
            new BackEase(),
            new BounceEase(),
            new CircleEase(),
            new CubicEase(),
            new ElasticEase(),
            new ExponentialEase(),
            new PowerEase(),
            new QuadraticEase(),
            new QuarticEase(),
            new QuinticEase(),
            new SineEase()
            };

            int eFindex = new Random().Next(0, ef.Count());
            int eFindex2 = new Random().Next(0, ef.Count());

            // ef[eFindex].EasingMode = EasingMode.EaseIn;
            // ef[eFindex].EasingMode = EasingMode.EaseOut;
            ef[eFindex].EasingMode = EasingMode.EaseInOut;

            DoubleAnimation doubleAnimationY = new DoubleAnimation()
            {
                Duration = new Duration(TimeSpan.FromSeconds(1)),
                EasingFunction = ef[eFindex]
            };

            DoubleAnimation doubleAnimationX = new DoubleAnimation()
            {
                Duration = new Duration(TimeSpan.FromSeconds(1)),
                EasingFunction = ef[eFindex2]
            };

            Storyboard sb = new Storyboard();

            this.RenderTransform = new TranslateTransform();

            doubleAnimationY.From = FromY;
            doubleAnimationY.To = ToY;

            doubleAnimationX.From = FromX;
            doubleAnimationX.To = ToX;

            Storyboard.SetTargetProperty(doubleAnimationY, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
            Storyboard.SetTargetProperty(doubleAnimationX, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));

            Storyboard.SetTarget(doubleAnimationY, this);
            Storyboard.SetTarget(doubleAnimationX, this);

            sb.Children.Add(doubleAnimationY);
            sb.Children.Add(doubleAnimationX);

            sb.Completed += new EventHandler(sb_MoveCompleted);
            sb.Begin();
        }

        public void sb_MoveCompleted(object sender, EventArgs e)
        {
            isMoving = false;
        }
    }
}
